package torres.entity;

import torres.fileIO.BasicContainer;
import torres.fileIO.ClassContainer;
import torres.map.Location;
import torres.map.Map;
import torres.map.World;
import torres.view.ImageLoader;

/**
 * @author Torres
 * Represents the player character and includes user-related statistics.
 */
public class Avatar extends Entity {
	
	private boolean showActionMenu = false;
	private Entity interactedWith;
	
	/**
	 * Creates a new default Avatar
	 */
	public Avatar(){
		
		super();
		observation = 4;
		name = "llama";
		imageName = name + "E.png";
		loader = new ImageLoader(imageName);
		image = loader.getImage();
		lives = 3;
		movement = 1;
		
		
	}
	
	/**
	 * The main avatar constructor which makes the avatar.
	 * @param displayName the name of the avatar
	 * @param job the occupation of the avatar {SNEAK, SUMMONER, SMASHER}
	 */
	public Avatar(String displayName, Occupation occupation) {
		
		super(occupation);
		if (!displayName.isEmpty())
			super.displayName = displayName;
		else 
			super.displayName = "Player";
		
		observation = 4;
		name = "llama";
		imageName = name + "E.png";
		loader = new ImageLoader(imageName);
		image = loader.getImage();
		lives = 3;
		movement = 1;
	}
	
	/* (non-Javadoc)
	 * @see torres.entity.Entity#interact(torres.entity.Entity)
	 */
	public void interact(Entity e) {
		if(((NPC)e).isHostile()) {
			Map combatArena = world.getCombatArena();
			
			if(!combatArena.equals(world.getCurrentMap())) {
				
				world.removeEntity(getLocation());
				world.removeEntity(e.getLocation());
	
				world.getCurrentMap().removeEnemy((NPC)e);
				world.getCombatArena().addEnemy((NPC)e);
				
				world.setCurrentMap(combatArena);
				
				Location location1 = new Location(2, 2);
				Location location2 = new Location(8, 8);
				world.addEntity(this, location1);
				world.addEntity(e, location2);
				
				setLocation(location1);
				
				((NPC) e).setBelongTo(combatArena);
				e.setLocation(location2);
			}
		}
	}
	
	/* (non-Javadoc)
	 * @see torres.entity.Entity#doMoreChecking(torres.map.Location)
	 */
	public boolean doMoreChecking(Location location) {
		
		if(World.getInstance().isOccupied(location)) {
			Entity e = World.getInstance().getEntity(location);
			
			if(!e.equals(this)) {
				e.interact(this);
			}
		}
		
		if(world.getObstacle(location) == null && world.getTerrain(location).getLandCost() <= movement) {
			
			if(world.hasAreaEffect(location)) {
				
				world.getAreaEffect(location).applyEffect(this);
			}
			
			return true;
		}
		else {
			
			return false;
		}
	}
	
	/**
	 * Toggles the Action Menu
	 */
	public void showActionMenu() {
		showActionMenu = !showActionMenu;
	}
	
	/**
	 * Returns the Action Menu of Avatar
	 * @return Action Menu
	 */
	public boolean getActionMenu() {
		return showActionMenu;
	}

	/* (non-Javadoc)
	 * @see torres.entity.Entity#save()
	 */
	@Override
	public ClassContainer save() {
			ClassContainer curClass = super.save();
			curClass.setName("Avatar");
			curClass.addProperty(new BasicContainer("showActionMenu",showActionMenu));
			if(interactedWith!=null)
				curClass.addProperty(interactedWith.save());
			
			return curClass;
	}

	/* (non-Javadoc)
	 * @see torres.entity.Entity#load(torres.fileIO.ClassContainer)
	 */
	@Override
	public void load(ClassContainer property) {
		super.load(property);
		BasicContainer showActionMenuCont = (BasicContainer)property.getProperty("showActionMenu");
		showActionMenu = Boolean.parseBoolean((String)showActionMenuCont.getValue());
		ClassContainer curTest;
		if((curTest=(ClassContainer)property.getProperty("NPC"))!=null)
			interactedWith.load(curTest);
		else if((curTest=(ClassContainer)property.getProperty("Merchant"))!=null)
			interactedWith.load(curTest);
	}
	
	/**
	 * Sets interactedWith of Avatar.
	 * @param interactedWith interactedWith String
	 */
	public void setInteractedWith(Entity interactedWith) {
		this.interactedWith = interactedWith;
	}
	
	/**
	 * Return interactedWith of Avatar
	 * @return interactedWith
	 */
	public Entity getInteractedWith() {
		return interactedWith;
	}
	
	public void moveHook() {
		this.getOccupation().moveHook(this);
	}
}
